<?php

/*
 * Table/AvenantHistoryTable.php
 */

namespace RHX\Model\Table;

use Zend\Db\Adapter\Adapter;
use Zend\Db\TableGateway\AbstractTableGateway;
use Zend\Db\ResultSet\ResultSet;
use RHX\Model\Object\AvenantHistory;
use Zend\Db\Sql\Select;
use Zend\Db\Sql\Where;

class AvenantHistoryTable extends AbstractTableGateway {

    protected $table = 'rh_avenant_history';

    public function __construct(Adapter $adapter) {
	$this->adapter			 = $adapter;
	$this->resultSetPrototype	 = new ResultSet();
	$this->resultSetPrototype->setArrayObjectPrototype(new AvenantHistory($adapter));
	$this->initialize();
    }

    public function getAll() {
	return $this->select();
    }

    public function get($id) {
	$row = $this->select(array('id' => $id))
		->current();
	if (!$row)
	    throw new \Exception("Impossible de trouver #$id");

	return $row;
    }

    /**
     * Recherche de l'Avenant d'un employé
     * 
     * @param type $matricule
     * @param type $order
     * @return ResultSet
     */
    public function getByMatricule($matricule, $order = 'avenant_id DESC') {
	return $this->select(function(Select $select) use($matricule, $order) {
			    $where = new Where();
			    $where->equalTo('matricule', $matricule);
			    //$where->equalTo('num', 1);
			    $select->where($where);
			    $select->order($order);
			});
    }
    
    /*
     * Recuperer un enregistrement par le champ avenant
     * @return AvenantHistory
     */
    public function getByAvenant($idAvenant) {
	$row = $this->select(array('avenant_id' => $idAvenant))
		->current();
	if (!$row)
	    throw new \Exception("Impossible de trouver #$id");

	return $row;
    }

    public function save(AvenantHistory $history) {
	$data	 = array(
	    'matricule'	 => $history->matricule,
	    'avenant_id'	 => $history->avenant_id,
	    'num'		 => $history->num,
	    'commentaire'	 => $history->commentaire,
	);
	$id	 = (int) $history->id;
	if ($id == 0) {
	    $this->insert($data);

	    return $this->lastInsertValue;
	}
	elseif ($this->get($id)) {
	    $this->update($data, array('id' => $id));

	    return $id;
	}
	else {
	    throw new \Exception("Impossible de selectionner #$id");
	}
    }

    public function erase($id) {
	return $this->delete(array('id' => $id));
    }

    /**
     * Changement des anciens matricules avec le nouveau matricule
     * 
     * @param int $old
     * @param int $new
     */
    public function updateHistoryMatricule($old, $new) {
	$oldHistory = $this->select(function(Select $select)use($old) {
		    $where = new Where();
		    $where->equalTo('matricule', $old);
		    $select->where($where);
		});
	foreach ($oldHistory as $history) {
	    $history->matricule = $new;
	    $this->save($history);
	}
    }
    
     public function getInitialByMatricule($matricule) {
	$sql = "SELECT * FROM rh_avenant_history
                    WHERE id = (SELECT MIN(id) FROM rh_avenant_history WHERE matricule= $matricule ) ";
        
        $statement = $this->adapter->query($sql);
        $result =  $statement->execute();
        $current = $result->current();
        
        $historyAvenant = $this->get($current['id']);
        
        return $historyAvenant;
    }
    
    /* Recupère le dernier AvenantHistory d'un employé 
     * @param int $matricule matricule d'un employé
     * @return AvenantHistory
     */
    public function getLastInsertByEmploye($matricule){
        $sql = "SELECT MAX(id) as id FROM rh_avenant_history 
                        WHERE matricule = $matricule ";
        
        $stt = $this->adapter->query($sql);
        $res = $stt->execute();   
        $cur = $res->current();
        
        $oAvHist = $this->get($cur['id']);
        return $oAvHist ;
    }

}

?>
